﻿(function (angular) {

    angular.module("app.dictionary-grid")
        .controller("Dictionary", Dictionary);

    Dictionary.$inject = ["wordData"];

    function Dictionary(wordData) {
        var vm = this;

        vm.words = [];
        vm.searchWord = "";

        wordData.getWords().then(function (data) {
            vm.words = data;
        });

        vm.deleteWord = function (wordId, index) {
            wordData.deleteWord(wordId).then(function () {
                vm.words.splice(index, 1);
            });
        };

        vm.checkWord = function (word) {
            if (!vm.searchWord) {
                return true;
            }

            var foreign = word.ForeignWord.toLowerCase()
            var translation = word.Translation.toLowerCase()
            var searchWord = vm.searchWord.toLowerCase();

            return foreign.indexOf(searchWord) > -1 || translation.indexOf(searchWord) > -1;
        };
    };

})(angular)